package com.thz.houserental.domain.user;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thz.houserental.infrastructure.auth.AuthUtil;
import com.thz.houserental.infrastructure.auth.UserDetail;
import com.thz.houserental.infrastructure.table.User;
import com.thz.houserental.infrastructure.web.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "用户信息管理")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    // TODO admin
    @GetMapping("/list")
    @Operation(summary = "分页查询用户信息")
    public IPage<UserVo> queryPage(UserVo userVo, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(StrUtil.isNotEmpty(userVo.getNickName()), User::getNickName, userVo.getNickName())
                .like(StrUtil.isNotEmpty(userVo.getPhone()), User::getPhone, userVo.getPhone());

        return userService.selectPageVo(new Page<>(pageNo, pageSize), wrapper);
    }

    @GetMapping("/{userId}")
    @Operation(summary = "获取用户详细信息")
    public UserVo getUserDetail(@PathVariable String userId) {
        return this.userService.getUserVo(userId);
    }
    
    @GetMapping("/update")
    @Operation(summary = "修改当前登录用户信息", description = "userId不需要传，后台会根据token自动填充")
    public void setUserDetail(@RequestBody User user) {
        this.userService.updateUserDetail(user);
    }
}
